$(function () {
    var fn = ABUS.Fn;
    var param = window.__param__;
    __param__.pageType = 'zuzhang';
    var dataList = [];

    $("#order_info").renderTemp({
        data:param
    });
    fn.countDown(param.remainTime, function (time, min) {
        $("#order_info [data-time]").text(min+"分钟")
    });
    fn.get({
        url: "/common/order/childOrders",
        data: param,
        success: function (data) {
            if (data.result !== 1) return false;

            $("#progress_list_1").renderTemp(data);
            $(".moving_line_2").each(function () {
                $(this).height($(this).parents("li").height() - $(this).prev('div').height())
            });
            new IScroll('#progress_list_1', {mouseWheel: true});
            $(".progress_list [data-time]").each(function () {
                var that = $(this);
                fn.countDown(that.data("time"), function (time, min) {
                    that.text('余:'+min + '分钟')
                })
            });
            dataList = data = data.data;

            $('.start_list').on("touchend", function () {
                fn.get({
                    url: "/common/order/acceptChildOrder",
                    data: {
                        orderId: param.orderId,
                        childOrderId: $(this).parents("li").data("id")
                    },
                    success: function (data) {
                        if (data.result === 1) {
                            fn.page_goto({
                                url: "./zuzhang_shouye_carPhoto.html",
                                data:__param__
                            })
                        }
                    }
                })
            });

            $(".addBtn:first").on("touchend", function () {
                __param__.stepIndex = data.length;
                fn.page_goto({
                    url: "../shouye/zuzhang_shouye_customize.html",
                    data:__param__
                })
            })
        }
    });

    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
        isDrag = true;
    }, {passive: false});

    //出现问题
    $(document).on("touchend",".question_button",function () {
        var idx = $(this).parents('li').index();
        var _param =  {
            orderId	:param.orderId,//订单id
            childOrderId:dataList[idx].id,//	子订单id
            remainTime:param.remainTime,//	剩余时间
            problemRemark:dataList[idx].problemRemark,
            childOrderRemainTime:dataList[idx].childOrderRemainTime?dataList[idx].childOrderRemainTime:""
        };

        fn.get({
            url:"/common/order/beforeSetProblemOrder",
            data:_param,
            success:function (data) {
                if(data.result === 1){
                    fn.page_goto({
                        url:"./zuzhang_banli_problemRemarks.html",
                        data:_param
                    })
                }else{
                    fn.alert({
                        type:"error",
                        title:data.message
                    })
                }
            }
        })
    });

    //完成子订单
    $(document).on("touchend",".finish_button",function () {
        // __param__.id = $(this).parents("li").attr("data-id");
        // __param__.childOrderRemainTime = $(this).parents("li").attr("data-childOrderRemainTime");
        // __param__.orderAcceptedPicUrl =
        var _param = $.extend(dataList[ $(this).parents("li").data("index")], __param__);
        fn.page_goto({
            url:"../../yewuyuan/banli/yewuyuan_banli_stopTask.html",
            data:_param
        });
    });

    //开始子订单
    $(document).on("touchend",".start_button:not(.finish_button)",function () {
        var that = this;
        var _param = $.extend(dataList[ $(this).parents("li").data("index")], __param__);
        fn.confirm({
            onSubmit: function () {
                __param__.id = $(that).parents("li").attr("data-id");
                __param__.childOrderRemainTime = $(that).parents("li").attr("data-childOrderRemainTime");
                fn.get({
                    url: "/common/order/startChildOrder",
                    data: {
                        orderId: __param__.orderId,
                        childOrderId: __param__.id
                    },
                    success: function (data) {
                        if (data.result === 1) {
                            fn.page_goto({
                                url: "../../yewuyuan/banli/yewuyuan_banli_stopTask.html",
                                data: _param
                            });
                            fn.closeModal();
                        } else {
                            fn.alert({
                                type: "error",
                                title: data.message
                            });
                        }
                    }
                })
            },
            onCancel:function () {
                fn.closeModal();
            }
        })
    });
    //查看子订单详情
    $(document).on("touchend",".progress_content",function () {
        if($(this).parents("li").attr("data-childOrderStatus") == '0'){
            return false;
        }
        if (isDrag) {
            isDrag = false;
            return true;
        }
        __param__.touchCurrentChildOrderId = $(this).parents("li").attr("data-id");
        __param__.childOrderRemainTime = $(this).parents("li").attr("data-childOrderRemainTime");
        __param__.childOrderStatus = $(this).parents("li").attr("data-childOrderStatus");
        // __param__.childOrderName = $(this).find("span").text();
        // __param__.childOrderState = $(this).siblings(".progress_status span").text()
        fn.page_goto({
            url:"./zuzhang_banli_taskDetails.html",
            data:__param__
        });

    });

    //结算
    $(document).on("touchend",".settle_button",function () {
        var that = this;
        fn.confirm({
            onSubmit: function () {
                fn.closeModal();
                setTimeout(function () {
                    var idx = $(that).index(".settle_button");
                    var returnedFee = prompt('请输入金额');
                    if(!returnedFee || returnedFee.search(/\D/)!==-1){
                        return false;
                    }
                    param.returnedFee = returnedFee;
                    param.childOrderId = $(that).parents("li").attr("data-id");//	子订单id
                    param.childOrderRemainTime = $(that).parents("li").attr("data-childOrderRemainTime");
                    param.isPaiedChildOrder = 1;
                    fn.get({
                        url:"/leader/order/endOrder",
                        data:{
                            orderId:param.orderId,
                            returnedFee:returnedFee
                        },
                        success:function (data) {
                            if(data.result === 1){
                                fn.page_goto({
                                    url:"./zuzhang_banli_leaderSettle.html",
                                    data:param
                                })
                            }else{
                                fn.alert({
                                    type:"error",
                                    title:data.message
                                })
                            }
                        }
                    })
                },500)
            },
            onCancel:function () {
                fn.closeModal();
            }
        })
    });

    //电话
    $(document).on("touchend",".user_information a",function () {
        window.location = $(this).attr("href");
    })

    //添加备注
    $('#bottom1').on("touchend", function () {
        fn.get({
            url:"/leader/order/beforeAddRemark",
            param:param,
            success:function (data) {
                if(data.result === 1){
                    fn.page_goto({
                        url:"./zuzhang_banli_OneHourRemarks.html",
                        data:param
                    })
                }else{
                    fn.alert({
                        type:"error",
                        title:data.message
                    })
                }
            }
        })
    });

    //请求救援
    $('#bottom2').on("touchend", function () {

        fn.confirm({
            onSubmit: function () {
                fn.get({
                    url:"/leader/order/helping",
                    data:{
                        orderId:param.orderId
                    },
                    success:function (data) {
                        if(data.result === 1){
                            window.history.back();
                        }
                    }
                })
                fn.closeModal();
            },
            onCancel: function () {

                fn.closeModal();
            }
        })

    });

    //出现问题
    $('#bottom3').on("touchend", function () {
        fn.page_goto({
            url:"./zuzhang_banli_viewProblemRemarks.html",
            data:param
        })
    });
    //出现问题
    if(!param.finishTime){
        if(param.situation === '-1'){
            $("#bottom3").show()
        }else if(param.remainTime > 30 && param.remainTime < 60){
            $("#bottom1").show()
        }else if(param.remainTime < 30){
            $("#bottom2").show()
        }
    }
});